#include"threadpoll.hh"
#include"Task.hh"
#include<iostream>
using std::thread;

ThreadPool::ThreadPool(size_t capa, size_t num)
    :_capa(capa)
     ,_thread()
     ,_queuenum(num)
     ,_queue(num)
     ,_isexit(false)
{

}
    void ThreadPool::start()
{
    //创建进程并阻塞
    for(size_t i= 0 ;i<_capa;i++)
    {
        _thread.emplace_back(thread(&ThreadPool::dowork,this));
    }
    return;
}

    void ThreadPool::dowork()
{

    while(true)
    {
    element pwork=_queue.pop();
    if(pwork)
    {
    pwork->process();
    }
    else
    {
        std::cout<<" 0 == queuesize "<<std::endl;
    }
    if(_isexit)
        break;
    }
}
    void ThreadPool::addTask(element rhs)
{
    _queue.push(rhs);
    return;
}

    void ThreadPool::wakeup()
{
    _queue.wakeup();
    
}
    //成员函数
    void ThreadPool::stop()
{
    while(!_queue.empty())
    {
    }

    std::this_thread::sleep_for(std::chrono::seconds(2));
    wakeup();
    _isexit=true;
    for(size_t i=0;i<_capa;i++)
    {
        _thread[i].join();
    }
}
